package cn.amaake.controller.student;


import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import cn.amaake.common.AmaakePage;
import cn.amaake.common.Result;
import cn.amaake.common.model.Course;
import cn.amaake.common.model.Student;
import cn.amaake.common.model.StudentCourse;
import cn.amaake.service.CourseService;
import cn.amaake.service.CourseServiceImp;

public class StuCourseController extends Controller {

	@Inject(CourseServiceImp.class)
	private CourseService courseService;
	
	public void setcourse() {
		Student student = getSessionAttr("stusession");
		setAttr("studentid", student.getId());
		render("/views/stuweb/course/setcourse.html");
	}
	
	public void setCoursePage() {
		int studentid = getParaToInt("studentid");
		int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        Page<Course> coursePage = courseService.getCourseStudentPage(pageNumber, pageSize, studentid, name);
        Result result = new Result();
        result.setData(new AmaakePage(coursePage));
        renderJson(result);
	}
	
	public void getcourse() {
		Student student = getSessionAttr("stusession");
		setAttr("studentid", student.getId());
		render("/views/stuweb/course/getcourse.html");
	}
	
	public void getCoursePage() {
		int studentid = getParaToInt("studentid");
		int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        Page<cn.amaake.common.model.Result> resultPage = cn.amaake.common.model.Result.dao.getStudentResultPage(pageNumber, pageSize, name, studentid);
        Result result = new Result();
        result.setData(new AmaakePage(resultPage));
        renderJson(result);
	}
	
	public void setstudentcourse() {
		Student student = getSessionAttr("stusession");
		int courseid = getParaToInt("courseid");
		StudentCourse studentCourse = new StudentCourse();
		studentCourse.setStudentId(student.getId());
		studentCourse.setCourseId(courseid);
		studentCourse.setClassId(student.getClassId());
		boolean boo = studentCourse.save();
		if(boo) {
			renderJson(Ret.ok("msg","选课成功！"));
		}else {
			renderJson(Ret.fail("msg","选课失败！"));
		}
	}
}

